/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.action.connect;

import org.zhiqim.account.ZacConstants;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.core.Action;
import org.zhiqim.kernel.util.Strings;
import org.zhiqim.kernel.util.Urls;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.manager.dao.ZmrParamDao;

/**
 * 微信公众号自动登录
 *
 * @version v1.4.1 @author zouzhigang 2018-4-20 新建与整理
 */
public class WxmpAction implements Action, ZacConstants
{
    public void execute(HttpRequest request) throws Exception
    {
        String appId = ZmrParamDao.getString(ZAC_NAME, WXMP_APP_ID);
        String callback = ZmrParamDao.getString(ZAC_NAME, WXMP_CALLBACK);
        
        if (Validates.isEmptyBlank(appId) || Validates.isEmptyBlank(callback))
        {
            request.returnHistory("系统未配置微信开放平台的[appId]或[callback]信息，请联系管理员在后台系统参数中配置");
            return;
        }
        
        String path = Strings.trimLeft(request.getRequestURI(), request.getRootPath("/connect/wxmp"));
        String url = Strings.formatMessage(WXMP_URL_AUTH, appId, Urls.encodeUTF8(callback + path), ZAC_NAME);
        request.getResponse().sendRedirect(url);
    }

}
